package com.tour.controller;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.tour.util.Upload;
import com.tour.util.CommonVal;
@Controller
@RequestMapping("/commonapi/imgUpload")
public class ImgUploadController{
    
    @RequestMapping(value="imgUploadForWangEditor",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> imgUploadForWangEditor(@RequestParam CommonsMultipartFile[] files,HttpServletRequest request) throws UnknownHostException{
        if(files==null){
            return null;
        }
        List<String> urls = new ArrayList<String>();
        Map<String,Object> result = new HashMap<String,Object>();
        String realUploadPath= "";  String uriPath= "";
        if(CommonVal.imgRealPath.equals("")==false){
            realUploadPath = CommonVal.imgRealPath;
            String [] split = CommonVal.imgRealPath.split("webapps");
            if(split.length>1){
                uriPath = split[1];
            }
            }else{
                realUploadPath= request.getSession().getServletContext().getRealPath("/")+"images";//使用tomcat文件路径作为上传路径
                uriPath= "tour_gl_sys/images";
            }
            for(CommonsMultipartFile f:files){
                String imageUrl;
                try {
                    imageUrl = Upload.uploadImage(f, realUploadPath,uriPath);
                    urls.add("http://"+imageUrl);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                result.put("data", urls);
                result.put("errno", 0);
                return result;
            }
            
        }
